﻿@using SelfManagement.Web.Models.ViewModels.DailyModels;
@model DailyViewModel

@{
    bool isEdit = Model.IsEdit;
    bool isDeferred = Model.Status == TaskStatus.Deffered;
    string title = isEdit ? "编辑" : "新建";
    
}

@section Styles{
    @*@Styles.Render(BundleConfig.TasksCssBundle)*@
    <link rel="stylesheet" type="text/css" href="~/assets/plugins/bootstrap-timepicker/compiled/timepicker.css" />
}

@section Scripts{
    @Scripts.Render(BundleConfig.UEditorSimple)
    @Scripts.Render(BundleConfig.TasksJsBundle)
    <script type="text/javascript" src="~/assets/plugins/bootstrap-timepicker/js/bootstrap-timepicker.js"></script>

    <script src="~/assets/plugins/jquery.tmpl.js"></script>
    <script id="newTask" type="text/x-jquery-tmpl">
        <li class="${DoneClass}" data-id="${Id}">
            <div class="task-checkbox">
                <div class="checker"><span class="${Checked}"><input type="checkbox" class="liChild" value=""></span></div>
            </div>
            <div class="task-title">
                <span class="task-title-sp">
                    ${TaskName}
                </span>
                <span class="label label-sm" data-time="${Time}">
                </span>
            </div>
            <div class="task-config">
                <div class="task-config-btn btn-group">
                    <button class="btn btn-xs btn-danger cancelTask">
                        <i class="fa fa-trash-o"></i>取消任务
                    </button>
                </div>
            </div>
        </li>
    </script>
    <script type="text/javascript">

        $(function () {

            function getTimeClass(minute) {
                if (typeof (minute) === 'string') {
                    minute = parseInt(minute);
                }
                if(!minute)
                    return "success";
                if (minute < 30)
                    return "danger";
                if (minute < 180)
                    return "warning";
                if (minute < 360)
                    return "primary";
                return "success";
            }

            function getTimeStr(source) {
                if (typeof (source) === 'string') {
                    source = parseInt(minute);
                }
                if(source == 0){
                    return "不限时";
                }
                var hour = Math.floor(source / 60);
                var minute = source % 60;
                var result = "";
                if (hour > 0){
                    result += (hour + "小时");
                }
                if(minute){
                    result += (minute + "分钟");
                }
                return result + "内完成";
            }

            var classes = "success,primary,warning,danger,defualt".split(',');

            function renderTask($li) {
                var $this = $li.find('.label-sm').first();
                var minute = $this.data('time');
                for (var i = 0, len = classes.length; i < len; i++) {
                    var labelClass = 'label-' + classes[i];
                    if ($this.is('.' + labelClass)) {
                        $this.removeClass(labelClass);
                    }
                }
                $this.addClass('label-' + getTimeClass(minute));
                $this.text(getTimeStr(minute));
            }


            var $subTaskName = $('#subTaskName');
            var $time = $('#time');


            Tasks.initDashboardWidget();

            var minutes = 14400;

            var tasks = [
                @foreach(var task in Model.Tasks.OrderBy(t => t.Order))
                {
                    var done = task.Status == TaskStatus.Completed;
                    <Text>
                    {
                        TaskName: '@task.TaskName',
                        DoneClass: '@(done ? "task-done": "")',
                        Id: '@task.Id',
                        Checked: '@(done ? "checked": "")',
                        Time: @task.EstimateTime,
                    },
                </Text>
                }
            ];
            $("#newTask").tmpl(tasks).appendTo('#tasks'); // 初始化所有任务
            
            $('#tasks li').each(function () {
                renderTask($(this));
            });

            $('#tasks').on('click', '.cancelTask', function () { // 取消任务
                $(this).closest('li').remove();
            });

            
            function initNewTask(){
                $subTaskName.val('');
                $time.val('');
            }
            
            $('#showNewTaskDiv').click(function () {
                initNewTask();
                $('#showNewTaskDiv').hide();
                $('#newTaskDiv').fadeIn(300, function(){
                    $subTaskName.focus();
                });
            });
            
            $('#addNewTask').click(function () { // 新增任务
                var subTaskName = $subTaskName.val();
                var time = $time.val();
                if(!time){
                    time = 0;
                }else{
                    time = time.split(':');
                    time = parseInt(time[0])* 60 + parseInt(time[1]);
                }
                if (!subTaskName) { return; }
                $('#newTask').tmpl([{ TaskName: subTaskName, DoneClass: '', Id: '', Checked: '', Time: time }]).appendTo('#tasks');
                renderTask($('#tasks li:last'));
                $('#newTaskDiv').fadeOut(300);
                $('#showNewTaskDiv').fadeIn(300);
                //initNewTask();
                //$subTaskName.focus();
            });

            
            $(document).keypress(function(e){
                if(e.keyCode == 13){
                    if(e.target.id == 'subTaskName'){
                        $('#addNewTask').click();
                        e.preventDefault();
                        return false;
                    }
                }
            });


            $('#cancelNewTask').click(function(){
                $('#newTaskDiv').fadeOut(300);
                $('#showNewTaskDiv').fadeIn(300);
            });

            window.getTaskJson = function () {
                var tasks = [];
                $('#tasks li').each(function () {
                    $this = $(this);
                    tasks.push({
                        Daily_Id: '@Model.Id',
                        Id: $this.data('id'),
                        TaskName: $this.find('.task-title-sp').text(),
                        Status: $this.is('.task-done') ? '@TaskStatus.Completed' : '@TaskStatus.Uncompleted',
                        EstimateTime: $this.find('.label-sm').data('time')
                    });
                });
                return JSON.stringify(tasks);
            }

            @if (Model.IsEdit)
            {
                <Text>
            UE.getEditor('@nameof(Model.Performance)');
            </Text>
            }

            window.beforeAjaxFormSubmit = function () {
                $('#TasksJson').val(window.getTaskJson());
                return true;
            }

            $('#time').timepicker({
                showMeridian: false,
                defaultTime: false,
            });
        });

    </script>
}

<!-- BEGIN FORM PORTLET -->
<div class="portlet">
    <div class="portlet-title">
        <div class="caption">
            <i class="fa fa-reorder"></i>@(title)每日计划
        </div>
    </div>
    <div class="portlet-body form">

        @using (Html.BeginForm("CreateOrEdit", "Daily", FormMethod.Post, new { @class = "form-horizontal js-ajax-form" }))
        {
            @Html.AntiForgeryToken()
            @Html.HiddenFor(m => m.Id)
            @Html.HiddenFor(m => m.IsEdit)
            @Html.HiddenFor(m => m.Status)
            @Html.HiddenFor(m => m.TasksJson)
            <div class="form-body">

                <div class="form-group">
                    @Html.LabelFor(m => m.TaskName, new { @class = "col-md-2 control-label" })
                    <div class="col-md-9">
                        @Html.TextBoxFor(m => m.TaskName, new { @class = "form-control", @placeholder = "一句话描述今日的任务" })
                        @Html.ValidationMessageFor(m => m.TaskName)
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(m => m.Date, new { @class = "col-md-2 control-label" })
                    <div class="col-md-9">
                        @if (isDeferred)
                        {
                            @Html.HiddenFor(m => m.Date)
                        }
                        else
                        {
                            <div class="input-group date form_datetime month">
                                <input type="text" name="@nameof(Model.Date)" value="@(Model.Date.ToString("yyyy-MM-dd"))" class="form-control" readonly />
                                <span class="input-group-btn">
                                    <button class="btn btn-success date-reset" type="button"><i class="fa fa-times"></i></button>
                                    <button class="btn btn-success date-set" type="button"><i class="fa fa-calendar"></i></button>
                                </span>
                            </div>
                        }
                    </div>
                </div>

                <h3 class="form-section"></h3>

                <div class="col-md-offset-2 col-md-8">
                    <div class="portlet tasks-widget">
                        <div class="portlet-title">
                            <div class="caption">
                                <i class="fa fa-check"></i>任务清单
                            </div>
                        </div>
                        <div class="portlet-body">
                            <div class="task-content">
                                <div data-always-visible="1" data-rail-visible1="1">
                                    <!-- START TASK LIST -->
                                    <ul class="task-list" id="tasks">
                                        
                                    </ul>
                                    <!-- END START TASK LIST -->
                                </div>
                            </div>

                            <div class="task-footer">
                                <button id="showNewTaskDiv" type="button" class="btn btn-success icn-only margin-right-10">新建任务<i class="fa fa-plus"></i></button>
                                <div class="chat-form" id="newTaskDiv" style="display:none">
                                    <div class="form-group col-md-12">
                                        <input type="text" placeholder="任务描述" class="form-control" id="subTaskName"/>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <div class="input-group form-inline" style="display:inline-block">
                                            <input type="text" readonly placeholder="估计用时" class="form-control" id="time" style="width: 120px;" />
                                            <button class="btn btn-default margin-right-10" type="button" id="clockface_2_toggle"><i class="fa fa-clock-o"></i></button>
                                        </div>
                                        <div class="input-group" style="display:inline-block">
                                            <button id="addNewTask" type="button" class="btn btn-primary icn-only margin-right-10">确定</button>
                                            <button id="cancelNewTask" type="button" class="btn btn-default icn-only">取消</button>
                                        </div>
                                    </div>

                                    @*<div class="input-cont">
        <input class="form-control" type="text" id="taskName" placeholder="新增任务...">
    </div>
    <div class="btn-cont">
        <span class="arrow">
        </span>
        <button id="addTask" type="button" class="btn btn-primary icn-only"><i class="fa fa-plus"></i></button>
    </div>*@
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="clearfix">
                </div>




                @if (Model.IsEdit)
                {

                    <h3 class="form-section">总结</h3>

                        <div class="form-group">
                            @Html.LabelFor(m => m.Performance, new { @class = "col-md-2 control-label" })
                            <div class="col-md-9">
                                <script id="@nameof(Model.Performance)" name="@nameof(Model.Performance)" type="text/plain" style="width:100%;height:200px;">
                                    @Html.Raw(Model.Performance)
                                </script>
                            </div>
                        </div>
                    if (Model.Status != TaskStatus.Deffered)
                    {
                        <div class="form-group">
                            <label class="col-md-2 control-label">
                                @Html.DisplayNameFor(m => m.IsCompledted)
                            </label>
                            <div class="col-md-9">
                                <div class="make-switch" data-on-label="是" data-off-label="否">
                                    @Html.CheckBoxFor(m => m.IsCompledted, new { @class = "toggle" })
                                </div>
                            </div>
                        </div>
                    }

                }

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-9">
                        @Html.ValidationSummary()
                    </div>
                </div>
            </div>
                <div class="form-actions fluid">
                    <div class="col-md-offset-3 col-md-9">
                        <button type="submit" class="btn btn-success">保存</button>
                        <a href="@HttpContext.Current.Request.UrlReferrer" class="btn btn-default">取消</a>
                    </div>
                </div>
        }
    </div>
</div>